package testerbeans;

import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import controllers.InterviewController;

import dao.InterviewDAORemote;
import dto.CandidateViewDto;
import dto.InterviewDTO;
import dto.InterviewViewDto;

@ManagedBean(name = "correctTestBean")
@SessionScoped
public class CorrectTestBean implements Serializable {
	// this bean is used for the correction of test, when the tester clicks the
	// "View" button
	@EJB
	InterviewDAORemote interviewBean;
	@ManagedProperty(value = "#{interviewController}")
	private InterviewController interviewController;
	private static final long serialVersionUID = 1L;
	private String name;
	private InterviewViewDto interview;
	private float totalScore;
	private String comment;
	private Boolean showTable;
	private String date;
	private int employeeid;
	private CandidateViewDto candidate;

	public void setNewComment() {
		// writes comment in database
		interviewBean.setComment(interview.getId(), comment);
		// set buttons
		interview.setDelete(true);
		interview.setSend(true);
		interview.setView(true);
		interview.setButDel("buttonDis");
		interview.setButSend("buttonDis");
		interview.setButView("buttonDis");
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect(
					"/MSGCandidateTesting/faces/testerhome.xhtml");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// reschedule the interview
	public void reschedule() {
		InterviewDTO idto = new InterviewDTO();
		idto = interviewBean.getInterview(interview.getId());
		String[] day= idto.getDate().toString().split(" ");
		String dayName="";
		if (day[0].equals("Mon")) dayName="Monday";
		if (day[0].equals("Tue")) dayName="Tuesday";
		if (day[0].equals("Wed")) dayName="Wednesday";
		if (day[0].equals("Thu")) dayName="Thursday";
		if (day[0].equals("Fri")) dayName="Friday";
		if (day[0].equals("Sat")) dayName="Saturday";
		if (day[0].equals("Sun")) dayName="Sunday";
			
		interviewController.setIdto(idto);
		interviewController.setSelectedTemplateID(idto.getTemplate().getId());
		interviewController.setSelectedRoomID(idto.getRoom().getId());
		Calendar cal = Calendar.getInstance();
		cal.setTime(idto.getDate());
		String aux="";
		if (cal.get(Calendar.MONTH) < 10) {
			aux = "0";
		}
		System.out.println(idto.getDate());
		String s = dayName +" - "+cal.get(Calendar.DAY_OF_MONTH) + "/"+aux+""+(cal.get(Calendar.MONTH) + 1) + "/"
		+ cal.get(Calendar.YEAR);
		System.out.println("selDate is: "+s);
		String[] start= idto.getStartTime().toString().split(":");
		String[] end= idto.getEndTime().toString().split(":");
		interviewController.setSelectedStartHour(Integer.parseInt(start[0]));
		interviewController.setSelectedStartMinute(Integer.parseInt(start[1]));
		interviewController.setSelectedEndHour(Integer.parseInt(end[0]));
		interviewController.setSelectedEndMinute(Integer.parseInt(end[1]));
		interviewController.setErrorMessage("");
		interviewController.setReschedule(true);
		interviewController.setEmployeeId(employeeid);
		interviewController.setSelDate(s);
		candidate.setId(idto.getCandidate().getId());
		interviewController.setCandidate(candidate);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect(
					"/MSGCandidateTesting/faces/addTest.xhtml");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setInterview(InterviewViewDto interview) {

		this.interview = interview;
	}

	public InterviewViewDto getInterview() {
		return interview;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setTotalScore(float totalScore) {
		this.totalScore = totalScore;
	}

	public float getTotalScore() {
		return totalScore;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getComment() {
		return comment;
	}

	public void setShowTable(boolean showtable) {
		this.showTable = showtable;
	}

	public boolean isShowTable() {
		return showTable;
	}

	public int getTestQuestionsSize() {
		return interview.getTestQuestions().size();
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDate() {
		return date;
	}

	public void setInterviewController(InterviewController interviewController) {
		this.interviewController = interviewController;
	}

	public InterviewController getInterviewController() {
		return interviewController;
	}

	public void setEmployeeid(int employeeid) {
		this.employeeid = employeeid;
	}

	public int getEmployeeid() {
		return employeeid;
	}

	public void setCandidate(CandidateViewDto candidate) {
		this.candidate = candidate;
	}

	public CandidateViewDto getCandidate() {
		return candidate;
	}

}
